package com.example.asus.a17376037_zq_market;

import java.io.IOException;

import Info.User;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import com.google.gson.Gson;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import util.HttpUtil;



public class LonginActivity extends Activity implements OnClickListener{
    EditText et_id, et_password;
    Button btn_login, btn_register;
    CheckBox cb_remember;
    User user = new User();
    static int header;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_longin);
        init();
    }
    private void init() {
        et_id = findViewById(R.id.et_id);
        et_password =  findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);
        btn_register = findViewById(R.id.btn_register);
        cb_remember = findViewById(R.id.cb_remember);

        boolean isRemeber;
        SharedPreferences pref = getSharedPreferences("user", MODE_PRIVATE);
        isRemeber = pref.getBoolean("isRemeber", false);
        if (isRemeber) {
            cb_remember.setChecked(true);
            int id = pref.getInt("id", 0);
            String aa = pref.getString("password", null);
            et_id.setText(id + "");
            et_password.setText(aa);
        }
        //点击监听
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
            case R.id.btn_login:
                String a = et_id.getText().toString();
                if (isNumber(a) || (et_password.getText().toString()).isEmpty()
                        || (et_id.getText().toString()).isEmpty()) {
                    Toast.makeText(getApplicationContext(), "账号或密码格式错误",
                            Toast.LENGTH_SHORT).show();
                }

                else {
                    checkUser();
                }

                break;
            case R.id.btn_register:
                Intent in = new Intent(LonginActivity.this, RegisteredActivity.class);
                startActivity(in);
                break;

            default:
                break;
        }
    }
    private void checkUser() {
        // TODO Auto-generated method stub
        header=Integer.parseInt(et_id.getText().toString());
        user.setId(header);
        user.setPassword(et_password.getText().toString());

        /****************** 连接服务 **********************************/
        String path ="http://106.13.133.171:80/MarketServer/Login?id="
                + user.getId() + "&password=" + user.getPassword();

        HttpUtil.sendHttpRequest(path, new Callback() {
            @Override
            public void onResponse(Call call, Response response)
                    throws IOException {
                // TODO Auto-generated method stub
                String responseData = response.body().string();
                Gson gson = new Gson();
                User user = gson.fromJson(responseData,User.class);
                showToast(user);

            }


            private void showToast(final User user) {
                // TODO Auto-generated method stub
                runOnUiThread(new Runnable() {
                    public void run() {
                        /* 得到服务端返回数据，并将信息判断，是否账号密码正确 */
                        if (user.getId() == -1) {
                            Toast.makeText(getApplicationContext(),
                                    user.getPassword(), Toast.LENGTH_SHORT)
                                    .show();
                        } else if (user.getId() == -2) {
                            Toast.makeText(getApplicationContext(),
                                    user.getPassword(), Toast.LENGTH_SHORT)
                                    .show();
                        } else {
                            Intent in = new Intent(getApplicationContext(),
                                    MainActivity.class);
                            startActivity(in);

                            /************************ 记住密码 ***********/
                            if (cb_remember.isChecked()) {
                                SharedPreferences pref = getSharedPreferences(
                                        "user", MODE_PRIVATE);
                                Editor editor = pref.edit();
                                editor.putBoolean("isRemeber", true);
                                editor.putInt("id", user.getId());
                                editor.putString("password", user.getPassword());
                                editor.apply();
                            } else {
                                SharedPreferences pref = getSharedPreferences(
                                        "user", MODE_PRIVATE);
                                Editor editor = pref.edit();
                                editor.putBoolean("isRemeber", false);
                                editor.apply();
                            }
                        }
                    }
                });
            }

            @Override
            public void onFailure(Call arg0, IOException arg1) {
                // TODO Auto-generated method stub

            }
        });
    }
    public int getUserId(){
        return header;

    }

    public boolean isNumber(String header) {
        for (int i = header.length(); --i >= 0;) {
            if (!Character.isDigit(header.charAt(i))) {
                return true;
            }
        }
        return false;
    }
}
